/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.jdbc.dao;

import com.thienloc.omap.jdbc.entity.SmsBroadcasting;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author GiangLT
 */
public class SmsBeforeProcessedDao extends BaseDao {
    
    protected DbAccess dbAccess = DbAccess.getInstance();

    public List<String> findMsisdnByDay(Date startDate, Date endDate) throws Exception {
        List<String> result = new ArrayList<String>();
        PreparedStatement ps = null;
        Connection conn = null;
        ResultSet rs = null;
        try {
            SimpleDateFormat partitionDf = new SimpleDateFormat("yyyyMM");
            String partitionName = "P" + partitionDf.format(startDate);
            String sql =
                    "select distinct MSISDN from TBL_SMS_BEFORE_PROCESSED "
                    + " partition (" + partitionName + ")"
                    + " where received_time >= ? and received_time <= ? ORDER BY MSISDN";
            conn = dbAccess.getConn();
            ps = conn.prepareStatement(sql);

            SimpleDateFormat dateFormat = new SimpleDateFormat(SmsBroadcasting.DATE_PATTERN);
            ps.setString(1, dateFormat.format(startDate));
            ps.setString(2, dateFormat.format(endDate));

            rs = ps.executeQuery();
            while (rs.next()) {
                result.add(rs.getString(1));
            }
        } catch (Exception ex) {
            throw ex;
        } finally {
            DbAccess.closeResultSet(rs);
            DbAccess.closePreparedStatement(ps);
            DbAccess.closeConn(conn);
        }

        return result;
    }

    public List<String> findMsisdnByDay(Date date) throws Exception {
        Date startDate = new Date(date.getTime());
        startDate.setHours(0);
        startDate.setMinutes(0);
        startDate.setSeconds(0);
        
        Date endDate = new Date(date.getTime());
        endDate.setHours(23);
        endDate.setMinutes(59);
        endDate.setSeconds(59);
        
        return findMsisdnByDay(startDate, endDate);
    }
}
